package com.lzgcoding.onlineshopapp.feature.splash

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import com.lzgcoding.onlineshopapp.R
import com.lzgcoding.onlineshopapp.nav.AppRoute
import com.lzgcoding.onlineshopapp.ui.theme.LargePadding
import com.lzgcoding.onlineshopapp.ui.theme.OnlineShopAppTheme

@Composable
fun SplashScreen(
    navigateToAppRoute: (AppRoute) -> Unit
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(
                brush = Brush.verticalGradient(
                    colors = listOf(
                        Color(0xFFfbfdfa),
                        Color(0xFFdeefdd),
                    )
                )
            )
            .statusBarsPadding()
            .navigationBarsPadding()
            .padding(horizontal = LargePadding),
        verticalArrangement = Arrangement.SpaceAround,
    ) {
        Text(
            text = stringResource(R.string.str_dress),
            style = MaterialTheme.typography.titleLarge,
            fontWeight = FontWeight.Bold,
            modifier = Modifier.align(Alignment.CenterHorizontally)
        )

        Image(
            painter = painterResource(R.drawable.intro_pic),
            contentDescription = "intro_pic",
            contentScale = ContentScale.Fit,
            modifier = Modifier.fillMaxWidth()
        )

        Column(
            modifier = Modifier.fillMaxWidth(),
            verticalArrangement = Arrangement.spacedBy(LargePadding)
        ) {
            Text(
                text = stringResource(R.string.str_intro_label),
                style = MaterialTheme.typography.titleLarge,
                fontWeight = FontWeight.Bold,
                lineHeight = 36.sp
            )

            Button(
                onClick = {
                    navigateToAppRoute(AppRoute.MainNav)
                },
                modifier = Modifier.fillMaxWidth(),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color(0xFF19c563)
                )
            ) {
                Text(text = stringResource(R.string.str_intro_get_start))
            }

            Text(
                text = buildAnnotatedString {
                    append(stringResource(R.string.str_intro_has_account))
                    append(" ")
                    withStyle(
                        style = SpanStyle(
                            color = Color(0xFF9cdab2)
                        )
                    ) {
                        append(stringResource(R.string.str_sign_in))
                        addStringAnnotation(
                            tag = "sign in",
                            annotation = "sign in",
                            start = length - stringResource(R.string.str_sign_in).length,
                            end = length
                        )
                    }
                },
                style = MaterialTheme.typography.labelLarge,
                modifier = Modifier.align(Alignment.CenterHorizontally)
            )
        }
    }
}


@Preview(showBackground = true, showSystemUi = true)
@Composable
private fun SplashScreenPreview() {
    OnlineShopAppTheme {
        SplashScreen(navigateToAppRoute = {})
    }
}